<template>
  <div class="songs-container">
    <div class="tab-bar">
    </div>
    <!-- 底部的table -->
    <table class="el-table playlit-table">
      <thead>
        <th></th>
        <th></th>
        <th>音乐标题</th>
        <th>歌手</th>
        <th>专辑</th>
        <th>时长</th>
      </thead>
      <tbody>
        <tr class="el-table__row" v-for="(item, index) in songs" :key="index" @dblclick="playMusic(item.id,item.name,item.picUrl)">
          <td>{{ index + 1 }}</td>
          <td>
            <div class="img-wrap">
              <img :src="item.song.album.picUrl+'?param=130y130'" alt="" />
              <span class="iconfont icon-play" @click="playMusic(item.id,item.name,item.picUrl)"></span>
            </div>
          </td>
          <td>
            <div class="song-wrap">
              <div class="name-wrap">
                <span>{{ item.name }}</span>
                <span v-if="item.song.mvid !== 0" class="iconfont icon-mv" @click="toMv(item.song.mvid)"></span>
              </div>
            </div>
          </td>
          <td>{{ item.name }}</td>
          <td>{{ item.song.album.name }}</td>
          <td>{{ item.song.duration | playTimeFormat }}</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'songs',
  data () {
    return {
      // 音乐列表
      songs: []
    }
  },

  created () {
    // 获取最新音乐
    this._getNewSongs()
  },

  methods: {
    // 获取最新音乐
    async _getNewSongs () {
      const {data: resp0} = await this.getNewSongs()
      this.songs = resp0.result
      // console.log(resp0.result.song.duration)
    },
    getNewSongs () {
      return axios.post('/personalized/newsong/50')
    },

    // 播放音乐
    async playMusic (id, name, picUrl) {
      // 获取歌曲播放地址
      const { data: resp } = await this.getMusicUrl(id)
      if (!resp.data[0].url) {
        return this.$message.error('该资源为VIP专享，暂不支持播放 ！')
      }
      // 设置给父组件的播放地址
      this.$parent.musicUrl = resp.data[0].url
      this.$parent.namees = name
      this.$parent.imgUrl = picUrl
    },
    getMusicUrl (id) {
      return axios.post(`/song/url/${id}`)
    },

    // 进入mv详情页面
    toMv (id) {
      this.$router.push(`/mv?id=${id}`)
    }
  }
}
</script>

<style >
</style>
